package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Purchase;
import com.woniu.supermarket.entity.PurchaseCheck;
import com.woniu.supermarket.entity.PurchaseDetail;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IPurchaseService;
import com.woniu.supermarket.vo.PurchaseVo;
import com.woniu.supermarket.vo.SearchPurchaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("purchase")
@Api(value = "采购操作")
public class PurchaseController {

    @Autowired
    IPurchaseService iPurchaseService;

    @PostMapping("/showPurchase")
    @ApiOperation(value = "分页查询操作")
    public ResultResponse<PageInfo<PurchaseVo>> showPurchase(@RequestBody SearchPurchaseVo searchPurchaseVo){
        ResultResponse<PageInfo<PurchaseVo>> result = null;
        try {
            PageInfo<PurchaseVo> pageInfo = iPurchaseService.findByCons(searchPurchaseVo.getPageNum(), searchPurchaseVo.getPageSize(), searchPurchaseVo);
            result = new ResultResponse<>(200,"查询采购订单成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询采购订单失败");
        }
        return result;
    }




    @GetMapping("/showPurchase2")
    @ApiOperation(value = "分页查询操作")
    public ResultResponse<PageInfo<PurchaseVo>> showPurchase2(@RequestParam(name = "now",defaultValue = "1") Integer now,
                                                              @RequestParam(name = "size",defaultValue = "3") Integer size){
        ResultResponse<PageInfo<PurchaseVo>> result = null;
        try {
            PageInfo<PurchaseVo> pageInfo = iPurchaseService.findByCons2(now,size);
            result = new ResultResponse<>(200,"查询采购订单成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询采购订单失败");
        }
        return result;
    }


    @PostMapping("/addPurchase")
    @ApiOperation(value = "添加采购订单操作")
    public ResultResponse<PurchaseVo> addPurchase(@ApiParam(value = "接受订单对象")@RequestBody PurchaseVo purchaseVo){
        ResultResponse<PurchaseVo> result = null;

        try {
            int i  = iPurchaseService.addPurchaseOrder(purchaseVo);
            if(i>0){
                result = new ResultResponse<>(200,"添加采购订单成功",purchaseVo);
            }else{
                result = new ResultResponse<>(201,"添加采购订单失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }


    @PutMapping("/modifyPurchase")
    @ApiOperation(value = "修改订单操作")
    public ResultResponse<Void> modify(@RequestBody PurchaseVo purchaseVo) {
        ResultResponse<Void> result = null;
        try {
            iPurchaseService.modifyPurchase(purchaseVo);
            result = new ResultResponse<>(200, "修改订单成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改订单失败");
        }
        return result;
    }


    @DeleteMapping("/delPurchase/{id}")
    @ApiOperation(value = "删除订单操作")
    public ResultResponse<Void> delPurchase(@PathVariable("id")Integer id) {
        ResultResponse<Void> result = null;
        try {
            iPurchaseService.removePurchase(id);
            result = new ResultResponse<>(200, "删除订单成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除订单失败");
        }
        return result;
    }


    @DeleteMapping("/delBatch")
    @ApiOperation(value = "批量删除订单操作")
    public ResultResponse<Void> delBatch(@RequestParam("ids")int[] ids) {
        ResultResponse<Void> result = null;
        try {
            iPurchaseService.removeBatchIds(ids);
            result = new ResultResponse<>(200, "批量删除订单成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "批量删除订单失败");
        }
        return result;
    }



    @GetMapping("/showAllPurchase")
    @ApiOperation(value = "查询所有采购单号")
    public ResultResponse<List<PurchaseVo>> showAllPurchase(){
        ResultResponse<List<PurchaseVo>> result = null;
        try {
            List<PurchaseVo> all = iPurchaseService.findAll();
            result = new ResultResponse<>(200,"查询采购单号成功",all);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询采购单号失败");
        }
        return result;
    }


    @GetMapping("/showPurchaseAndDetailOfOne/{purchaseNumber}")
    @ApiOperation(value = "查询某个采购订单以及详情，根据采购订单号")
    public ResultResponse<PurchaseVo> showPurchaseAndDetailOfOne(@ApiParam(value = "采购单单号")@PathVariable String purchaseNumber){
        ResultResponse<PurchaseVo> result = null;
        try {
            PurchaseVo purchaseVo = iPurchaseService.findPurchaseAndDetailByPurchaseNumber(purchaseNumber);
            if(purchaseVo != null){
                result = new ResultResponse<>(200,"查询采购顶单成功",purchaseVo);
            }else{
                result = new ResultResponse<>(200,"查询采购顶单失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }


    @PostMapping("/auditPurchase")
    @ApiOperation(value = "审核订单操作")
    public ResultResponse<PurchaseCheck> auditPurchase(@ApiParam(value = "审核信息对象")@RequestBody PurchaseCheck purchaseCheck){
        ResultResponse<PurchaseCheck> result = null;

        try {
            int i  = iPurchaseService.auditPurchaseCheck(purchaseCheck);
            if(i>0){
                result = new ResultResponse<>(200,"审核采购订单成功",purchaseCheck);
            }else{
                result = new ResultResponse<>(201,"审核采购订单失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }


}
